<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag type="list"  v-model="model.classId"  dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc" placeholder="请选择班级" :disabled="model.id != null"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentId">
              <j-dict-select-tag type="list"  v-model="model.studentId" :dictCode="`sk_student,name,id,del_flag=0 and class_id=${model.classId}`" placeholder="请选择学生" :disabled="model.id != null" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="筛查时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="screeningDate">
              <j-date  date-format="YYYY-MM-DD" placeholder="请选择筛查时间" v-model="model.screeningDate" style="width: 100%" :disabled="model.id != null" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="左眼视力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leftEyeVision">
              <a-input-number v-model="model.leftEyeVision" :min="0"  :precision="1" :max="5" placeholder="请输入左眼视力" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="右眼视力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rightEyeVision">
              <a-input-number v-model="model.rightEyeVision" :min="0" :precision="1" :max="5" placeholder="请输入体重右眼视力" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { ajaxGetDictItems,getDictItemsFromCache} from '@/api/api'

  export default {
    name: 'SkBmiRecordForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        statusCheck: true,
        bmiStandard: [],
        model:{
          status: 1,
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 },
        },
        confirmLoading: false,
        validatorRules: {
           classId: [
              { required: true, message: '请选择班级!'},
           ],
           studentId: [
              { required: true, message: '请选择学生!'},
           ],
           screeningDate: [
              { required: true, message: '请选择筛查时间!'},
           ],
           rightEyeVision: [
              { required: true, message: '请输入左眼视力!'},
           ],
           leftEyeVision: [
              { required: true, message: '请输入右眼视力!'},
           ]
        },
        url: {
          add: "/health/skVisionScreening/add",
          edit: "/health/skVisionScreening/edit",
          queryById: "/health/skVisionScreening/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictData();
    },
    methods: {
      initDictData() {
        //根据字典Code, 初始化字典数组
        ajaxGetDictItems('bmi_standard', null).then((res) => {
          if (res.success) {
            this.bmiStandard = res.result;
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        this.statusCheck = (record.status == 1) ? true : false;
      },
      onChose(checked) {
        if (checked) {
          this.model.status = 1;
          this.statusCheck = true;
        } else {
          this.model.status = 0;
          this.statusCheck = false;
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>